<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/18
 * Time: 14:55
 */
namespace Admin\Model;

use Think\Model;

class BannerModel extends Model
{
    protected $trueTableName = 'banners';

    public function bannersAdd()
    {
        $title = I('post.title');
        $description = I('post.description');
        $is_show = I('post.is_show');
        $size= I('post.size');
        $sort = I('post.sort');
        $origin_image = I('post.image');

        if (empty($title)) {
            $this->error = '标题必须填写';
            return false;
        }
        if (mb_strlen($title)>100) {
            $this->error = '标题长度不能超过100';
            return false;
        }
        if (empty($description)) {
            $this->error = '描述必须填写';
            return false;
        }
        if (mb_strlen($description)>100) {
            $this->error = '描述长度不能超过100';
            return false;
        }

        //if (empty($is_show)) {
        //    $this->error = '是否显示必须选择';
        //    return false;
        //}
        if (empty($origin_image)) {
            $this->error = '图片必须上传';
            return false;
        }
        $size = explode('x',$size);
        if ((!is_integer((int)$size[0])) or (!is_integer((int)$size[1]))) {
            $this->error = '图片大小输入的格式错误';
            return false;
        }
        $image = new \Think\Image();
        $image->open(APP_PATH.$origin_image);
        $mobile_image = '/thumb/'.date('Y-m-d', time()).'/';
        mkdir(APP_PATH.$mobile_image, 0777, true);
        $mobile_image = $mobile_image.basename($origin_image);
        //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        $image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH.$mobile_image);
        if (empty($sort)) {
            $this->error = '排序必须填写';
            return false;
        }
        if (is_integer($sort)) {
            $this->error = '排序必须是整数';
            return false;
        }

        $this->title = $title;
        $this->description = $description;
        $this->is_show = $is_show;
        $this->sort = $sort;
        $this->origin_image = $origin_image;
        $this->mobile_image = $mobile_image;
        $this->size = I('post.size');

        if ($this->add()) {
            return true;
        } else {
            return false;
        }

    }


    public function bannersModify()
    {
        $id = I('post.id');
        $title = I('post.title');
        $description = I('post.description');
        $is_show = I('post.is_show');
        $size= I('post.size');
        $sort = I('post.sort');
        $origin_image = I('post.image');

        //先找出需要修改的轮播图
        $banner = $this->find($id);

        if (empty($title)) {
            $this->error = '标题必须填写';
            return false;
        }
        if (mb_strlen($title)>100) {
            $this->error = '标题长度不能超过100';
            return false;
        }
        if (empty($description)) {
            $this->error = '描述必须填写';
            return false;
        }
        if (mb_strlen($description)>100) {
            $this->error = '描述长度不能超过100';
            return false;
        }
        //if (empty($is_show)) {
        //    $this->error = '是否显示必须选择';
        //    return false;
        //}
        if (empty($origin_image)) {
            $this->error = '图片必须上传';
            return false;
        }
        $size = explode('x',$size);
        if ((!is_integer((int)$size[0])) or (!is_integer((int)$size[1]))) {
            $this->error = '图片大小输入的格式错误';
            return false;
        }

        $mobile_image = $banner['mobile_image'];
        if ($banner['origin_image'] != $origin_image){
            unlink(APP_PATH.$banner['origin_image']);
            unlink(APP_PATH.$banner['mobile_image']);
            $image = new \Think\Image();
            $image->open(APP_PATH.$origin_image);
            $mobile_image = '/thumb/'.date('Y-m-d', time()).'/';
            mkdir(APP_PATH.$mobile_image, 0777, true);
            $mobile_image = $mobile_image.basename($origin_image);
            //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH.$mobile_image);
        } else {
           if ($banner['size'] != I('post.size')) {
               unlink(APP_PATH.$banner['mobile_image']);
               $image = new \Think\Image();
               $image->open(APP_PATH.$origin_image);
               $mobile_image = '/thumb/'.date('Y-m-d', time()).'/';
               mkdir(APP_PATH.$mobile_image, 0777, true);
               $mobile_image = $mobile_image.basename($origin_image);
               //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
               $image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH.$mobile_image);
           }
        }


        if (empty($sort)) {
            $this->error = '排序必须填写';
            return false;
        }
        if (is_integer($sort)) {
            $this->error = '排序必须是整数';
            return false;
        }

        $this->title = $title;
        $this->description = $description;
        $this->is_show = $is_show;
        $this->sort = $sort;
        $this->origin_image = $origin_image;
        $this->mobile_image = $mobile_image;
        $this->size = I('post.size');

        if ($this->where(array('id'=>array('eq',$id)))->save()) {
            return true;
        } else {
            return false;
        }

    }



}